package com.qst.crop.demo.controller;

import com.qst.crop.demo.common.Result;
//import com.qst.crop.demo.common.UserSessionUtils;
import com.qst.crop.demo.entity.Product;
import com.qst.crop.demo.entity.User;
import com.qst.crop.demo.service.ProductService;
import com.qst.crop.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/product")
@CrossOrigin(origins = "*")
public class ProductController {

    @Autowired
    ProductService productService;
    @Autowired
    UserService userService;

    /**
     *  增加商品
     * @param email
     * @param product
     * @return
     */
    @RequestMapping("/add")
    public Result add(String email, Product product){
        System.out.println("添加商品");

        if(product == null || product.getProductId() <= 0 || !StringUtils.hasLength(product.getProductName())){
            return Result.fail(-1, "无效参数,起码得给我个id和名字吧");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }

        if(userinfo.getIdentity().equals("administrator")){

            if(productService.add(product) == 1){
                return Result.success(200, "添加成功");
            }else{
                return Result.fail(-4, "该商品已经存在");
            }
        }else{
            return Result.fail(-3, "没有权限");
        }

    }

    /**
     *  搜索商品
     * @param searchWord
     * @return
     */
    @RequestMapping("/search")
    public Result select(String searchWord){
        System.out.println("搜索商品");
        if(searchWord == null){
            return Result.fail(-1, "非法请求");
        }

        List<Product> products = productService.select(searchWord);
        if(products.size() == 0){
            return Result.fail(-2, "未找到相关商品");
        }

        return Result.success(200, products);
    }

    /**
     *  根据id获得商品
     * @param productId
     * @return
     */
    @RequestMapping("/getProById")
    public Result getProById(Long productId){
        System.out.println("根据商品id获得商品");
        if(productId <= 0){
            return Result.fail(-1, "非法参数");
        }

        Product product = productService.getProById(productId);

        return Result.success(200, product);
    }

//    /**
//     *  获取全部商品
//     * @return
//     */
//    @RequestMapping("/getAllPro")
//    public Result getAllProducts(){
//        System.out.println("获取全部商品信息");
//
//        List<Product> products = productService.selectAll();
//
//        return Result.success(200, products);
//    }


    /**
     *  删除商品
     * @param email
     * @param product
     * @return
     */
    @RequestMapping("/del")
    public Result delete(String email, Product product){
        System.out.println("删除商品");

        if(product == null || product.getProductId() <= 0 ){
            return Result.fail(-1, "非法参数");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }

        if(userinfo.getIdentity().equals("administrator")){
            productService.delete(product);
        }else{
            return Result.fail(-3, "没有权限");
        }

        return Result.success(200, "删除成功");
    }

    /**
     *  根据商品id修改商品
     * @param product
     * @return
     */
    @RequestMapping("/update")
    public Result update(String email, Product product){
        System.out.println("修改商品");

        if(product == null || product.getProductId() <= 0) {
            return Result.fail(-1, "非法参数");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }

        if(userinfo.getIdentity().equals("administrator")){

            if(productService.update(product) == 0){
                return Result.fail(-3, "商品不存在");
            }else{
                return Result.success(200, "修改成功");
            }
        }else{
            return Result.fail(-4, "没有权限");
        }
    }

}
